package com.kaifamiao.demo.d0902;

import java.sql.*;
import java.util.Date;

public class Query {

    public static void main(String[] args) {
        try {
            // 加载类，注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 建立连接
            String url = "jdbc:mysql://localhost:3306/kfm";
            Connection connection = DriverManager.getConnection(url, "root", "");
            // 创建 statement 对象
            Statement statement = connection.createStatement();
            // 定义 sql
            String sql = "select id, username, password, createdate from users";
            ResultSet resultSet = statement.executeQuery(sql);
            // 解析结果
            if (resultSet != null){
                while(resultSet.next()){
                    // resultSet 代表一行数据
                    int id = resultSet.getInt(1);
                    String username = resultSet.getString("username");
                    String password = resultSet.getString(3);
                    Date date = resultSet.getTimestamp("createdate");
                    System.out.println(id + "\t" + username + "\t" + password + "\t" + date);
                }
            }


            // 关闭连接
            resultSet.close();
            statement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
